package com.paul.sys.controller.back;

import com.paul.sys.model.Ptstaff;
import com.paul.sys.model.Syrole;
import com.paul.sys.service.SyroleService;
import com.paul.sys.tools.PageData;
import com.paul.sys.util.ParamUtil;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * Created by yingg_000 on 2015/7/13.
 */
@Controller
@RequestMapping("/back/role/")
public class RoleController {
    private static Logger logger = Logger.getLogger(RoleController.class);
    @Autowired
    private SyroleService roleService;

    @RequestMapping("ismenu/role")
    public String role() throws Exception {
        return "back/syrole/role-list";
    }

    @ResponseBody
    @RequestMapping("list")
    public Map<String, Object> listRole(HttpServletRequest request) {

        Map<String, Object> result = new HashMap<String, Object>();

        try {
            ParamUtil util = new ParamUtil(request);
            Map<String, String> param = util.getParamMap();
            PageData<Syrole> roles = roleService.listByPage(param);
            result.put("totalRows", roles.getPageBean().getTotalNum());
            result.put("curPage", param.get("curPage"));
            result.put("data", roles.getListData());
            result.put("success", true);
        } catch (Exception e) {
            logger.error(e.getMessage());
            e.printStackTrace();
        }

        return result;
    }

    /**
     * 根据角色的id获得菜单的权限树
     *
     * @param roleunid
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping("menutree")
    public Map<String, Object> getMenutreeByroleid(String roleunid) throws Exception {
        Map<String, Object> result = new HashMap<String, Object>();
        List<Map> list = roleService.getTreebyRole(roleunid);
        List<Map> list2 = new ArrayList<Map>();
        Map<String, Object> map = null;
        for (int i = 0; i < list.size(); i++) {
            map = new HashMap<String, Object>();
            map.put("id", list.get(i).get("id"));
            map.put("pId", list.get(i).get("pid"));
            map.put("name", list.get(i).get("name"));
            map.put("checked", "1".equals(list.get(i).get("checked")) ? true : false);
            list2.add(map);
        }
        result.put("status", 0);
        result.put("data", list2);
        return result;
    }

    @ResponseBody
    @RequestMapping("add_page")
    public ModelAndView addPage(String unid) {
        ModelAndView view = new ModelAndView("back/syrole/role-add");
        return view;
    }

    @ResponseBody
    @RequestMapping("Judgerolename")
    public Map<String, Object> roleJudge(HttpServletRequest request) throws Exception {
        Map<String, Object> result = new HashMap<String, Object>();

        ParamUtil util = new ParamUtil(request);
        Map<String, String> param = util.getParamMap();
        List<Map> Judgemes = roleService.roleNamejudge(param.get("param"));
        if (Judgemes.size() != 0) {
            result.put("info", "角色名重复 ");
        } else {
            result.put("status", "y");
        }
        return result;
    }

    @ResponseBody
    @RequestMapping(value = "doadd", method = RequestMethod.POST)
    public Map<String, Object> roleAdd(Syrole syrole) throws Exception {
        Map<String, Object> result = new HashMap<String, Object>();
        String unid = UUID.randomUUID().toString();
        syrole.setUnid(unid);
        syrole.setCreateby("admin");
        int n = this.roleService.insert(syrole);
        result.put("success", true);
        result.put("unid", n);
        return result;
    }

    @ResponseBody
    @RequestMapping("edit_page")
    public ModelAndView editPage(String unid) {
        ModelAndView view = new ModelAndView("back/syrole/role-edit");
        if (unid != null) {
            Syrole role = roleService.selectById(unid);
            view.addObject("role", role);
            view.addObject("type", "edit");
        }
        return view;
    }

    @ResponseBody
    @RequestMapping(value = "doupdate", method = RequestMethod.POST)
    public Map<String, Object> roleUpdate(Syrole symenu) throws Exception {
        Map<String, Object> result = new HashMap<String, Object>();
        roleService.update(symenu);
        result.put("status", "y");
        return result;
    }

    @ResponseBody
    @RequestMapping("delete")
    public String deleteUser(String unid) {
        String result = "true";
        try {
            roleService.delete(unid);
        } catch (Exception e) {
            result = "false";
            e.printStackTrace();
            logger.error(e.getMessage());
        }
        return result;
    }

    @ResponseBody
    @RequestMapping("deletes")
    public String deleteUsers(String unids) {
        String[] unidArr = unids.split(",");
        String result = "true";
        try {
            roleService.deletes(unidArr);
        } catch (Exception e) {
            result = "false";
            e.printStackTrace();
            logger.error(e.getMessage());
        }
        return result;
    }

    @ResponseBody
    @RequestMapping("usersByRole")
    public Map<String, Object> listPtstaffByRole(String unid, String curPage, String pageSize) {
        Map<String, Object> result = new HashMap<String, Object>();
        Map<String, String> parm = new HashMap<String, String>();
        parm.put("curPage", curPage);
        parm.put("pageSize", pageSize);
        try {
            PageData<Ptstaff> pageData = roleService.getUserbyRole(unid, parm);
            result.put("totalRows", pageData.getPageBean().getTotalNum());
            result.put("curPage", curPage);
            result.put("data", pageData.getListData());
            result.put("success", true);
        } catch (Exception e) {
            logger.error(e.getMessage());
            e.printStackTrace();
        }
        return result;
    }

    @ResponseBody
    @RequestMapping(value = "domenulink", method = RequestMethod.POST)
    public Map<String, Object> doRoleLink(@RequestParam String roleunid, @RequestParam String menuunids, @RequestParam String enable) throws Exception {
        Map<String, Object> result = new HashMap<String, Object>();
        Map<String, Object> param = new HashMap<String, Object>();
        param.put("roleunid", roleunid);
        param.put("menuunids", menuunids);
        param.put("enable", enable);
        int res = 0;
        if (enable.equals("1")) {
            res = roleService.linkMenu(param);
        } else {
            res = roleService.unlinkMenu(param);
        }
        result.put("success", true);
        return result;
    }
}
